﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xwang.Task.Model;

namespace Xwang.Task.MvcApp.Controllers
{
    /// <summary>
    /// 任务的增删改处理
    /// </summary>
    public class TaskController : AppController
    {
        //
        // GET: /Task/

        IBll.ITaskInfoService TaskInfoService { get; set; }

        public ActionResult Add(TaskInfo task)
        {
            try
            {
                task.Biller = LoginUserInfo.KeyId;
                task.BillDate = DateTime.Now;
                task.TaskDesc = "";
                TaskInfoService.Add(task);
                var count = TaskInfoService.Savechanges();
                return Json(new
                {
                    success = count > 0,
                    msg = count > 0 ? "ok" : "新增任务失败！"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    msg = ex.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult GetListByKind(int type)
        {
            var list = TaskInfoService.LoadEntities(t => t.TaskListKind == type && t.TaskStatus != 3);
            return Json(new
            {
                success = true,
                msg = "ok",
                data = from task in list
                       select new
                       {
                           task.KeyId,
                           task.TaskTitle,
                           UserName = task.UserInfo.UserName.Substring(0, 1).ToUpper(),
                           endtime = task.TaskEndTime == null ? "" : task.TaskEndTime.Value.ToString("yy-MM-dd"),
                           over = task.TaskEndTime == null ? 0 : task.TaskEndTime.Value.AddDays(1).CompareTo(DateTime.Now)
                       }
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetTask(int id)
        {
            var task = TaskInfoService.LoadEntities(t => t.KeyId == id).FirstOrDefault();
            return Json(new
            {
                success = true,
                msg = "ok",
                data = new
                {
                    task.KeyId,
                    task.TaskListKind,
                    task.TaskTitle,
                    task.TaskDesc,
                    users = from user in task.TaskJoinUser
                            select new
                            {
                                user.UserInfo.UserName,
                                user.UserId
                            }
                }
            }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新任务的标题及详细描述信息
        /// </summary>
        /// <param name="taskInfo"></param>
        /// <returns></returns>
        public ActionResult Update(TaskInfo taskInfo)
        {
            try
            {
                var task = TaskInfoService.LoadEntities(t => t.KeyId == taskInfo.KeyId).FirstOrDefault();
                task.TaskTitle = taskInfo.TaskTitle;
                task.TaskDesc = taskInfo.TaskDesc;
                var count = TaskInfoService.Savechanges();
                return Json(new
                {
                    success = count > 0,
                    msg = count > 0 ? "ok" : "更新任务失败！"
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    msg = ex.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Test1()
        {
            return View();
        }
    }
}
